import axios from 'axios'

function empty(){}
const defaultOption = {
  success: empty,
  error: empty,
  data: {},
  url: ''
}
var baseUrl = __dev__?'/api':'';

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

export default function(option) {
  var option = Object.assign({}, defaultOption, option);

  axios({
    method: 'POST',
    url: baseUrl + option.url,
    headers: {
      'Content-Type': 'application/json',
    },
    data: option.data,
  }).then(function(response) {
    console.log(response);
    const { data } = response;
    if (response.status == 200) {
      if(data.status == -1){
        window.location.href = data.data
      }else if(data.status == 1){
        option.success(data.data);
      }else{
        option.error(data.msg||'请求失败');
      }
    } else{
      option.error(data.msg||'请求失败');
    }
  }).catch(function (error) {
    console.log(error);
  });
}

